<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="entity.ImageItem" %>
<%@ page import="util.ItemHelper" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.net.URLEncoder"%>
<%!
	public String getNavHref(int offset, int number, String tag)
	{
	    if(tag != null){
	        return "?number=" + number + "&offset=" + offset + "&tag=" + tag;
	    }
	    else
	    { 
	        return "?number=" + number + "&offset=" + offset;
	    }
	}
%>
<% 
    //parse params
	String soffset = request.getParameter("offset");
	String snumber = request.getParameter("number");
	String tag = request.getParameter("tag");
	int offset = 0;
	int number = 10;
	if(soffset != null)
	{
	    try{
	    offset = Integer.parseInt(soffset);
	    }
	    catch(NumberFormatException e){;}
	}
	if(snumber != null)
	{
	    try{
	        number = Integer.parseInt(snumber);
	        }
	        catch(NumberFormatException e){;}
	}

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel='stylesheet' type='text/css' href='stylesheets/main.css' />
<title>The Ladder</title>
</head>
<body>
<%
  List<ImageItem> results = ItemHelper.getLadder(offset, number, tag);
//prev-next things
%>
  <div class='laddernav'>
	  <% if(offset > 0){%>
	      <a href='<%= getNavHref(0, number, tag)%>'>&lt;&lt;</a>
	      <a href='<%= getNavHref(Math.max(offset-number,0), number, tag)%>'>&lt;</a>
	  <%}else{%>
	      &lt;&lt;
	      &lt;
	  <% }%>
	  <% if(results.size() == number) {%>
	     <a href='<%= getNavHref(offset+number, number, tag)%>'>&gt;</a>
	  <% }else {%>
	     &gt;
	  <%} %>
  </div>
  <% 
  //table of results
  if(!results.isEmpty()){
      %>
      <table class=laddertable>
      <%
      int idx = 1 + offset;
      for(ImageItem item: results)
      {%>
          <%-- temporary output for testing --%>
          <tr>
            <td class=ladderank><%= idx %></td>
            <td><img title='<%= item.getTitle() %>' class='ladderimage' src="<%= item.getUrl() %>"></td>
            <td class='ladderrating'><%= item.getRating() %></td>
            <%-- probably should remove... --%>
            <td><%=item.getTags() %></td>
          </tr>
       <%
          idx += 1;
      }%>
      </table>
  <%}%>
</body>
</html>